package com.yellowpage.ca.action;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.InputValidator;
import com.yellowpage.white.util.PasswordUtil;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.UserService;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


public class UserAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	public String RESULT_CHANGE_PASSWORD = "changePassword";

	private Configuration configuration;

	private List<User> userList = new ArrayList<User>();
	private List<Map> resultList = new ArrayList<Map>();

	private User user;
	private UserService userService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_email;
	private String query_lastLoginTime;
	private String query_name;
	private String query_displayName;
	private String query_status;
	
	private String old_password;
	private String new_password;
	private String confirm_password;	
	

	public void prepare() {
		log.error("********* UserAction prepare().....", null);	
		log.error("********* UserAction prepare()...selectedId:" + this.selectedId, null);	
		
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.user = userService.getUser(Long.parseLong(selectedId));
			
			log.error("********* UserAction prepare()...this.user:" + this.user, null);	
		}
	}

	public String queryByPage() {
		log.error("********* UserAction queryByPage().....", null);			
				
		initPageRender();
		
		log.error("********* UserAction initPageRender().....", null);

		Map queryMap = new HashMap();
		queryMap.put("email", query_email);
		queryMap.put("lastLoginTime", query_lastLoginTime);		
		queryMap.put("name", query_name);
		queryMap.put("displayName", query_displayName);
		queryMap.put("status", query_status);

		log.error("********* UserAction userService.queryUserByPage()...userService :" + userService, null);
		
		resultList = userService.queryUserByPage(queryMap, pageRender);
		
		log.error("********* UserAction setPageRenderToRequest().....", null);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {
		
		log.error("********* UserAction save()....." + user.toString(), null);			
		
		user.setCreateTime(new Date());
		user.setCreateUser(loginUser.getUserId());	
		user.setLastUpdateTime(new Date());
		user.setLastUpdateUser(loginUser.getUserId());	
		user.setStatus(loginUser.getStatus());
		
		
		log.error("********* UserAction save()....." + user.getUserId(), null);	
		log.error("********* UserAction save()....." + user.getName(), null);	
		
		BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
		String password = passwordEncoder.encode(user.getPassword());
		user.setPassword(password);

		userService.saveUser(user);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (InputValidator.isEmpty(user.getEmail())) {
			this.addActionError("** 电子邮件 不能为空 !");
		}
		if (user.getName().length() == 0) {
			this.addActionError("** 名称 不能为空 !");
		}
		if (user.getDisplayName().length() == 0) {
			this.addActionError("** 显示名称 不能为空 !");
		}
		if (user.getPassword().length() == 0) {
			this.addActionError("** 密码 不能为空 !");
		}
		if (user.getStatus().length() == 0) {
			this.addActionError("** 状态 不能为空 !");
		}

		this.setValidatorResult("/ca/UserAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);
		
		user = userService.getUser(id);

		return RESULT_EDIT;
	}

	public String update() {
		
		log.error("********* UserAction update()....." + user.toString(), null);			
		
		
		user.setLastUpdateTime(new Date());
		user.setLastUpdateUser(loginUser.getUserId());	
		
		BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
		String password = passwordEncoder.encode(user.getPassword());
		user.setPassword(password);
		
		log.error("********* UserAction update()....." + user.getUserId(), null);	
		log.error("********* UserAction update()....." + user.getName(), null);	

		userService.updateUser(user);
		
		

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (user.getEmail().length() == 0) {
			this.addActionError("** 电子邮件 不能为空 !");
		}
		if (user.getName().length() == 0) {
			this.addActionError("** 名称 不能为空 !");
		}
		if (user.getDisplayName().length() == 0) {
			this.addActionError("** 显示名称 不能为空 !");
		}
		if (user.getPassword().length() == 0) {
			this.addActionError("** 密码 不能为空 !");
		}
		if (user.getStatus().length() == 0) {
			this.addActionError("** 状态 不能为空 !");
		}
		
		this.setValidatorResult("/ca/UserUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		this.userService.delete(ids);

		return queryByPage();
	}
	
	public String toChangePassword(){	

		user = userService.getUser(loginUser.getUserId());
		
		old_password = user.getPassword();
		
		return RESULT_CHANGE_PASSWORD;
	}
	
	public String saveChangePassword(){	
		
		user = userService.getUser(loginUser.getUserId());
			
		//String password = PasswordUtil.encodingByMd5(new_password);			
		
		BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
		String password = passwordEncoder.encode(new_password);
		
		user.setPassword(password);
		
		
				
		user.setLastUpdateTime(new Date());
		user.setLastUpdateUser(loginUser.getUserId());	
	
		userService.updateUser(user);

		message = "密码修改成功!";
		
		return RESULT_CHANGE_PASSWORD;
	}
	
	public void validateSaveChangePassword() {
		if (new_password.length() == 0) {
			this.addActionError("** 密码 不能为空 !");
		}else if (confirm_password.length() == 0) {
			this.addActionError("** 密码 不能为空 !");
		}else if (!confirm_password.equals(new_password)) {
			this.addActionError("** 您输入的密码不一致 !");
		}
		
	}
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_email() {
		return this.query_email;
	}

	public void setQuery_email(String email) {
		this.query_email = email;
	}

	public String getQuery_lastLoginTime() {
		return this.query_lastLoginTime;
	}

	public void setQuery_lastLoginTime(String lastLoginTime) {
		this.query_lastLoginTime = lastLoginTime;
	}	

	public String getQuery_name() {
		return this.query_name;
	}

	public void setQuery_name(String name) {
		this.query_name = name;
	}

	public String getQuery_displayName() {
		return this.query_displayName;
	}

	public void setQuery_displayName(String displayName) {
		this.query_displayName = displayName;
	}	

	public String getQuery_status() {
		return this.query_status;
	}

	public void setQuery_status(String status) {
		this.query_status = status;
	}

	public String getOld_password() {
		return old_password;
	}

	public void setOld_password(String old_password) {
		this.old_password = old_password;
	}

	public String getNew_password() {
		return new_password;
	}

	public void setNew_password(String new_password) {
		this.new_password = new_password;
	}

	public String getConfirm_password() {
		return confirm_password;
	}

	public void setConfirm_password(String confirm_password) {
		this.confirm_password = confirm_password;
	}
	
	


}
